<?php
 defined("BASEDIR") or die();
 
 class Application_Controller_Adm_User
 {
	protected $view, $request;
	public function __construct(Application_Router_Standard_Request $request)
	{
		$this->request = $request;
		$this->view = Application_View_ViewEngine::getInstance();
		if($request->show != "")
		{
			$actionName = $request->show."Action";
			if(method_exists($this, $actionName))
			{
				$this->$actionName();
				$this->view->assign("module", $request->action."_".$request->show."_admin");
			}
			else
				$this->view->assign("module", $request->action."_admin");
		}
		else
		{
			$db = Application_Model_DataBase::getInstance();
			$db->getTable("Pacjenci");
			$db->count();
			$db->execute();
			$num = $db->getResult();
			// $num[0] - Liczba wierszy w tabeli
			$pages = ceil($num[0]/constant("DISPLAY_LIMIT"));
			$this->view->assign("pages", $pages+1);
			$this->view->assign("totalUsers", $num[0]);
			
			if($this->request->offset == "") $offset = 0;
			else $offset = ($this->request->offset-1)*constant("DISPLAY_LIMIT");
			$users = array();
			$db->getTable("Pacjenci");
			if($this->request->user_pesel != "")
			{
				$ids = explode(",", $this->request->user_pesel);
				foreach($ids as $cid)
					$db->orWhere("pesel=".$cid);
			}
			if($this->request->user_imie != "") $db->where("imie='".$this->request->user_imie."'");
			if($this->request->user_nazwisko != "") $db->where("nazwisko='".$this->request->user_nazwisko."'");
			if($this->request->user_mail != "") $db->where("email='".$this->request->user_mail."'");
			if($this->request->user_search == "") $db->limit(constant("DISPLAY_LIMIT"), $offset);
			$db->execute();
			while($u = $db->getResult())
			{
				
				$users[] = $u;
			}
			$this->view->assign("userData", $users);
			$this->view->assign("module", $request->action."_admin");
		}
	}
	protected function detailsAction()
	{
		$db = Application_Model_DataBase::getInstance();
		
		$err = array();
		if(empty($err) && $this->request->user_pesel != "")
		{
			$db->getTable("Pacjenci", "UPDATE");
			$db->setFields("imie, nazwisko, email, telefon");
			$values = array($this->request->user_imie, $this->request->user_nazwisko, $this->request->user_mail, $this->request->user_telefon);
			$db->setValues(implode(",", $values));
			$db->where("pesel=".$this->request->user_pesel);
			$num = $db->execute();
			$this->view->assign("added", $num);
		}
		$db->getTable("Pacjenci");
		$db->where("pesel=".$this->request->user);
		$db->execute();
		$result = $db->getResult();
		
		$this->view->assign("user", $result);
	}
 }
?>